package com.liuxw.www.service;

import com.liuxw.www.bean.JsonResult;
import com.liuxw.www.bean.Liuxw;
import com.liuxw.www.dao.LiuxwDao;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.util.JSONPObject;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Service
public class LiuxwServiceImpl implements LiuxwService{

    @Resource
    private LiuxwDao liuxwDao;

    @Override
    @Transactional
    public JsonResult save(Liuxw liuxw) {
        liuxwDao.insert(liuxw);
        return new JsonResult();
    }

    @Override
    @Transactional
    public JsonResult delete(Object id) {
        liuxwDao.delete(id);
        return new JsonResult();
    }

    @Override
    @Transactional
    public JsonResult update(Liuxw liuxw) {
        liuxwDao.update(liuxw);
        return new JsonResult();
    }

    @Override
    public JsonResult select(Object id) {
        Liuxw liuxw = liuxwDao.select(id);
        return new JsonResult(liuxw);
    }

    @Override
    public JsonResult selectList(LinkedHashMap map) {
        List<Liuxw> list = liuxwDao.selectList(map);
        return new JsonResult(list);
    }

    @Override
    public JsonResult selectListFund() {
        List<Map<String, String>> list = new ArrayList<>();
        String fundCodeStr =
                "005176,519008,110031,270002," +
                "233005,163111,110020,040016," +
                "020001,000478,000051,040008," +
                "110017,000216,486002";

        String[] fundCodeStrArr = fundCodeStr.split(",");
        for (String funCode:fundCodeStrArr) {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            String url = "http://www.dayfund.cn/ajs/ajaxdata.shtml?showtype=getfundvalue&fundcode=" + funCode;
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse response = null;
            try {
                response = httpClient.execute(httpGet);
                String contentStr = EntityUtils.toString(response.getEntity());
                // System.out.println("funCode: " + funCode + "[" + contentStr + "]");
                // 封装数据
                String[] contentStrArr = contentStr.split("\\|");
                Map<String, String> map = new LinkedHashMap<>();
                map.put("fundCode", funCode); // 基金代码
                map.put("netDate", contentStrArr[0]); // 净值日期
                map.put("latestNet", contentStrArr[1]); // 最新净值
                map.put("grandTotalNet", contentStrArr[2]); // 累计净值
                map.put("growthValue", contentStrArr[3]); // 增长值
                map.put("growthRate", contentStrArr[4]); // 增长率
                list.add(map);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // System.out.println("list: " + list.toString());
        return new JsonResult(list);
    }

    @Override
    public JsonResult selectListFundTs() {
        List<String> list = new ArrayList<>();
        String fundCodeStr =
                "005176,519008,110031,270002," +
                        "233005,163111,110020,040016," +
                        "020001,000478,000051,040008," +
                        "110017,000216,486002";

        String[] fundCodeStrArr = fundCodeStr.split(",");
        for (String funCode:fundCodeStrArr) {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            String url = "http://fundgz.1234567.com.cn/js/" + funCode + ".js";
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse response = null;
            try {
                response = httpClient.execute(httpGet);
                String contentStr = EntityUtils.toString(response.getEntity(), "UTF-8");
                // System.out.println(contentStr);
                // 封装数据
                list.add(contentStr.replace("jsonpgz(", "").replace(");", ""));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // System.out.println("list: " + list.toString());
        return new JsonResult(list);
    }

    @Override
    public JsonResult selectFund(String fundCode) {
        List<Map<String, Object>> list = new ArrayList<>();

        CloseableHttpClient httpClient = HttpClients.createDefault();
        String url = "http://fundgz.1234567.com.cn/js/" + fundCode + ".js";
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            String contentStr = EntityUtils.toString(response.getEntity(), "UTF-8");
            // System.out.println(contentStr);
            // 封装数据
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(contentStr.replace("jsonpgz(", "").replace(");", ""));
            list.add(stringObjectMap(jsonNode.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // System.out.println("list: " + list.toString());
        return new JsonResult(list);
    }

    @Override
    public JsonResult selectListFundName(String fundName) {
        //
        List<Map<String, Object>> list = new ArrayList<>();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String url = "http://fundsuggest.eastmoney.com/FundSearch/api/FundSearchAPI.ashx?m=1&key=" + fundName;
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            String contentStr = EntityUtils.toString(response.getEntity(), "UTF-8");
            // System.out.println(contentStr);
            // 封装数据
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(contentStr);
            JsonNode jsonNode1 = jsonNode.get("Datas");
            for (JsonNode jsonNode2:jsonNode1) {
                // System.out.println(jsonNode2.toString());
                list.add(stringObjectMap(jsonNode2.toString()));
            }
            // System.out.println(list.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new JsonResult(list);
    }

    // String转Map
    public Map<String, Object> stringObjectMap(String mapStr) {
        if (mapStr.startsWith("{"))
            mapStr = mapStr.substring(1);
        if (mapStr.endsWith("}"))
            mapStr = mapStr.substring(0, mapStr.length() -1);
        Map<String, Object> map = new LinkedHashMap<>();
        String[] mapStrArr = mapStr.split(",");
        for (int i = 0; i < 8 && i < mapStrArr.length; i++) {
            String[] maps = mapStrArr[i].split(":");
            map.put(maps[0].replace("\"", ""),
                    maps[1].replace("\"", ""));
        }
        return map;
    }
}
